﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
//using Microsoft.Xna.Framework.Net;
//using Microsoft.Xna.Framework.Storage;

namespace SpriteDisplay
{
    /// <summary>
    /// Enemy klassen hanterar hur varje enskild fiende rör sig mot spelaren.
    /// </summary>
    class Enemy
    {
        Vector2 position; //position on the screen where the enemy currently is

        Texture2D picture; //a Texture2D to hold the sprite of the enemy

        Vector2 spriteOrigin;

        //Player tracking %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        //Vector2 position; finns redan
        Vector2 velocity, direction, player_pos;
        float angle;
        float distance;
        int baseSpeed;

        //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        //Variable for collision detection
        float radius = 40.0f;

        //collision detection schtuff --------------------------------
        public float Radius
        {
            get { return radius; }
        }

        public int CollisionBall(List<Bullet> BulletList)
        {
            for (int i = 0; i < BulletList.Count; i++)
            {
                if ((BulletList[i].Position - position).Length() < radius)
                    return i;
            }
            return -1;
        }

        //-----------------------------------------------------------

        //Konstruktor, when we create an enemy it needs a sprite, start position and speed
        public Enemy(Texture2D picture, Vector2 startPosition, Vector2 player_pos, int baseSpeed)
        {
            this.picture = picture;
            position = startPosition;
            this.baseSpeed = baseSpeed;
            this.player_pos = player_pos;

            //Setup origin
            spriteOrigin.X = (float)picture.Width / 2.0f;
            spriteOrigin.Y = (float)picture.Height / 2.0f;
        }

        public Vector2 Position
        {
            get { return position; }
        }

        //draws the enemy at its current position
        public void Draw(SpriteBatch batch)
        {
            batch.Draw(picture, position, null, Color.White, angle,
                new Vector2(spriteOrigin.X, spriteOrigin.Y), 1.0f, SpriteEffects.None, 0.0f);
        }


        public void Update(GameTime gameTime)
        {
            direction = player_pos - position; //gets the X,Y difference;
            distance = calculateDistance(player_pos, position);
            velocity = Vector2.Zero; //reset velocity;
            if (direction != Vector2.Zero)
            {
                //Convert to direction
                direction.Normalize();
            }
            if (distance < baseSpeed)
                velocity += direction * (2 * distance); //higher than 2 for speed increase
            else
                velocity += direction * (2 * baseSpeed); //higher than 2 for speed increase
            position += velocity;
            CalculateAngleAndDirection(position, player_pos, out direction, out angle);

        }

        public void update_player_pos(Vector2 player_pos)
        {
            this.player_pos = player_pos;
        }

        private void CalculateAngleAndDirection(Vector2 from, Vector2 to, out Vector2 direction, out float angle)
        {
            direction = to - from; //get the direction vector
            direction.Normalize(); //make it a unit vector
            angle = (float)Math.Atan2(-direction.X, direction.Y);
        }

        private float calculateDistance(Vector2 A, Vector2 B)
        {
            //A == player_pos, B == enemy pos
            //Math.Abs(-1) will output 1
            //Math.Abs(1) will output 1
            //If the player_pos is negative make it positive
            A = new Vector2(Math.Abs(A.X), Math.Abs(A.Y));
            B = new Vector2(Math.Abs(B.X), Math.Abs(B.Y));

            float Y_diff, X_diff, distance;

            Y_diff = A.Y - B.Y;
            X_diff = A.X - B.X;

            //We have calculated the X difference and Y difference
            //Now apply Pythagorean theorem to calculate the distance
            distance = (float)Math.Sqrt(Math.Pow(X_diff, 2) + Math.Pow(Y_diff, 2));

            return (float)1;
        }
    }
}
